package com.resourcestore.action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.*;
import com.resourcestore.model.*;
import com.resourcestore.utils.tag.Pagination;

public class ExpertMainAction extends BaseAction {

	private List<MajorGroup> listMajorGroup;
	private MajorGroupDao majorGroupDao = new MajorGroupDao();
	private MajorDao majorDao = new MajorDao();
	private List<Major> listMajor;
	private CourseCategoryDao courseCategoryDao = new CourseCategoryDao();
	private List<CourseCategory> listCourseCate;
	private CourseDao courseDao = new CourseDao();
	private List<Course> listCourse;
	private MajorGroup majorGroup;
	private Pagination pagination = new Pagination();
	private Major major;
	private ExcellentCourse excellentCourse;
	private ExcellentCourseDao excellentCourseDao = new ExcellentCourseDao();
	private List<MajorFileSrcCate> listMajorFileSrcCate;
	private MajorFileSrcCateDao majorFileSrcCateDao = new MajorFileSrcCateDao();
	private MajorFileSrcCate majorFileSrcCate;
	private Announce announce;
	private List<Announce> listAnnounce;
	private AnnounceDao announceDao = new AnnounceDao();
	private List<CourseFileSrcCatePublic> listCourseFileSrcCatePublic;
	private CourseFileSrcCatePublicDao courseFileSrcCatePublicDao = new CourseFileSrcCatePublicDao();
	private CourseFileSrcCatePublic courseFileSrcCatePublic;

	private List<CourseTeaClassFK> listCourseTeaClassFK;
	private CourseTeaClassFKDao courseTeaClassFKDao = new CourseTeaClassFKDao();
	private CourseTeaClassFK courseTeaFK;
	private List<Department> listDepartment;
	private Department department;
	private DepartmentDao departmentDao = new DepartmentDao();

	/**
	 * 首页内容
	 * 
	 */
	public String expertIndexContent() {
		listAnnounce = announceDao.selectAnnounce();

		for (int i = 0; i < listAnnounce.size(); i++) {
			try {
				listAnnounce.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listAnnounce.get(i)
								.getGmtCreated()));
				listAnnounce.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listAnnounce.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/ExpertMain/expertIndexContent.jsp");
	}

	/**
	 * 功能导航
	 * 
	 */
	public String expertTree(){
		
		//查询系所列表
		listDepartment = departmentDao.selectDepartment();
		
		//查询专业列表
		listMajor = majorDao.selectMajor();
		
		listCourseCate = new ArrayList<CourseCategory>();
		listCourse = new ArrayList<Course>();
		listMajorFileSrcCate = new ArrayList<MajorFileSrcCate>();
		listCourseFileSrcCatePublic = new ArrayList<CourseFileSrcCatePublic>();
		listCourseTeaClassFK = new ArrayList<CourseTeaClassFK>();
		
		for(int i =0;i<listMajor.size();i++){
			
			//预置专业文件分类
			MajorFileSrcCate mfscIsPreTemp = new MajorFileSrcCate();
			mfscIsPreTemp.setIsPreCategory(1);
			List<MajorFileSrcCate> listMFSCIsPreTemp = majorFileSrcCateDao.selectMajorFileSrcCateByIsPreAndMajorId(mfscIsPreTemp);
			for(int j=0;j<listMFSCIsPreTemp.size();j++){
				listMFSCIsPreTemp.get(j).setMajorId(listMajor.get(i).getMajorId());
				if(listMFSCIsPreTemp.get(j).getMajorFileCateParentId()==-1){
					listMFSCIsPreTemp.get(j).setMajorIdString("97"+String.valueOf(listMajor.get(i).getMajorId()));
					listMFSCIsPreTemp.get(j).setMajorFileCateParentIdString(null);
				}else{
					listMFSCIsPreTemp.get(j).setMajorIdString("94"+String.valueOf(listMajor.get(i).getMajorId()));
					listMFSCIsPreTemp.get(j).setMajorFileCateParentIdString(String.valueOf(listMFSCIsPreTemp.get(j).getMajorFileCateParentId()));
				}
				listMFSCIsPreTemp.get(j).setMajorFileCateName("<span class='isPreCate'>"+listMFSCIsPreTemp.get(j).getMajorFileCateName()+"</span>");
			}
			listMajorFileSrcCate.addAll(listMFSCIsPreTemp);
			
			//非预置专业文件分类
			mfscIsPreTemp = new MajorFileSrcCate();
			mfscIsPreTemp.setIsPreCategory(0);
			mfscIsPreTemp.setMajorId(listMajor.get(i).getMajorId());
			List<MajorFileSrcCate> listMFSCIsNotPreTemp = majorFileSrcCateDao.selectMajorFileSrcCateByIsPreAndMajorId(mfscIsPreTemp);
			for(int j=0;j<listMFSCIsNotPreTemp.size();j++){
				if(listMFSCIsNotPreTemp.get(j).getMajorFileCateParentId()==-1){
					listMFSCIsNotPreTemp.get(j).setMajorIdString("97"+String.valueOf(listMFSCIsNotPreTemp.get(j).getMajorId()));
					listMFSCIsNotPreTemp.get(j).setMajorFileCateParentIdString(null);
				}else{       
					listMFSCIsNotPreTemp.get(j).setMajorIdString("94"+String.valueOf(listMFSCIsNotPreTemp.get(j).getMajorId()));
					listMFSCIsNotPreTemp.get(j).setMajorFileCateParentIdString(String.valueOf(listMFSCIsNotPreTemp.get(j).getMajorFileCateParentId()));
				}
				listMFSCIsNotPreTemp.get(j).setMajorFileCateName("<span class='isNotPreCate'>"+listMFSCIsNotPreTemp.get(j).getMajorFileCateName()+"</span>");
			}
			listMajorFileSrcCate.addAll(listMFSCIsNotPreTemp);
			
			//课程分类
			CourseCategory courseCategoryTemp = new CourseCategory();
			courseCategoryTemp.setIsPublic(0);
			List<CourseCategory> listCourseCategoryTemp = courseCategoryDao.selectCourseCategoryByIsPublic(courseCategoryTemp);
			for(int j=0;j<listCourseCategoryTemp.size();j++){
				listCourseCategoryTemp.get(j).setMajorId(listMajor.get(i).getMajorId());
			}
			listCourseCate.addAll(listCourseCategoryTemp);
			
			
			//公共课程
			Course courseTemp = new Course();
//			courseTemp.setIsPublic(1);
//			List<Course> listCoursePublicTemp = courseDao.selectCourseByIsPublic(courseTemp);
//			for(int j=0;j<listCoursePublicTemp.size();j++){
//				listCoursePublicTemp.get(j).setMajorId(listMajor.get(i).getMajorId());
//			}
//			listCourse.addAll(listCoursePublicTemp);
			
			//专业课程
			courseTemp.setIsPublic(0);
			courseTemp.setMajorId(listMajor.get(i).getMajorId());
			List<Course> listCoursePrivateTemp = courseDao.selectCourseByIsPublic(courseTemp);
			listCourse.addAll(listCoursePrivateTemp);
			
			for(int j=0;j<listCourse.size();j++){
				
				//预置课程公共文件分类
				CourseFileSrcCatePublic cfscIsPreTemp = new CourseFileSrcCatePublic();
				cfscIsPreTemp.setIsPreCourseCate(1);
				List<CourseFileSrcCatePublic> listCFSCIsPreTemp = courseFileSrcCatePublicDao.selectCourseFileSrcCatePublicByIsPreAndCourseId(cfscIsPreTemp);
				for(int k=0;k<listCFSCIsPreTemp.size();k++){
					listCFSCIsPreTemp.get(k).setMajorId(listMajor.get(i).getMajorId());
					listCFSCIsPreTemp.get(k).setCourseId(listCourse.get(j).getCourseId());
					if(listCFSCIsPreTemp.get(k).getCourseFileCateParentId()==-1){
						listCFSCIsPreTemp.get(k).setMajorIdString("91"+String.valueOf(listMajor.get(i).getMajorId()));
						listCFSCIsPreTemp.get(k).setCourseFileCateParentIdString(null);
					}else{
						listCFSCIsPreTemp.get(k).setMajorIdString("90"+String.valueOf(listMajor.get(i).getMajorId()));
						listCFSCIsPreTemp.get(k).setCourseFileCateParentIdString(String.valueOf(listCFSCIsPreTemp.get(k).getCourseFileCateParentId()));
					}
				}
				listCourseFileSrcCatePublic.addAll(listCFSCIsPreTemp);
				
				//非预置课程公共文件分类
				cfscIsPreTemp = new CourseFileSrcCatePublic();
				cfscIsPreTemp.setIsPreCourseCate(0);
				cfscIsPreTemp.setCourseId(listCourse.get(j).getCourseId());
				List<CourseFileSrcCatePublic> listCFSCIsNotPreTemp = courseFileSrcCatePublicDao.selectCourseFileSrcCatePublicByIsPreAndCourseId(cfscIsPreTemp);
				for(int k=0;k<listCFSCIsNotPreTemp.size();k++){
					listCFSCIsNotPreTemp.get(k).setMajorId(listMajor.get(i).getMajorId());
					if(listCFSCIsNotPreTemp.get(k).getCourseFileCateParentId()==-1){
						listCFSCIsNotPreTemp.get(k).setMajorIdString("91"+String.valueOf(listMajor.get(i).getMajorId()));
						listCFSCIsNotPreTemp.get(k).setCourseFileCateParentIdString(null);
					}else{
						listCFSCIsNotPreTemp.get(k).setMajorIdString("90"+String.valueOf(listMajor.get(i).getMajorId()));
						listCFSCIsNotPreTemp.get(k).setCourseFileCateParentIdString(String.valueOf(listCFSCIsNotPreTemp.get(k).getCourseFileCateParentId()));
					}
				}
				listCourseFileSrcCatePublic.addAll(listCFSCIsNotPreTemp);
				
				
				//授课教师(这个有问题，待修改)
				CourseTeaClassFK ctFKTemp = new CourseTeaClassFK();
				ctFKTemp.setCourseId(listCourse.get(j).getCourseId());
				List<CourseTeaClassFK> listCTFKTemp = courseTeaClassFKDao.selectCourseTeaClassFKByCourseId(ctFKTemp);
				for(int k=0;k<listCTFKTemp.size();k++){
					listCTFKTemp.get(k).setMajorId(listMajor.get(i).getMajorId());
					listCTFKTemp.get(k).setCourseId(listCourse.get(j).getCourseId());
				}
				listCourseTeaClassFK.addAll(listCTFKTemp);		
			}
			
		}

		
		return newpath("/ExpertMain/expertTree.jsp");
	}

	/**
	 * 查询专业群信息BYid
	 * 
	 */
	public String infoMajorGroupReadById() {
		majorGroup = majorGroupDao.selectMajorGroupById(majorGroup);
		return newpath("/ExpertMain/infoMajorGroupReadById.jsp");
	}

	/**
	 * 查询专业信息BYid
	 * 
	 */
	public String infoMajorReadById() {
		major = majorDao.selectMajorById(major);
		return newpath("/ExpertMain/infoMajorReadById.jsp");
	}

	/**
	 * 查询精品课程信息BYid
	 * 
	 */
	public String infoExcellentCourseReadById() {
		excellentCourse = excellentCourseDao
				.selectExcellentCourseById(excellentCourse);
		return newpath("/ExpertMain/infoExcellentCourseReadById.jsp");
	}

	public List<MajorGroup> getListMajorGroup() {
		return listMajorGroup;
	}

	public void setListMajorGroup(List<MajorGroup> listMajorGroup) {
		this.listMajorGroup = listMajorGroup;
	}

	public List<Major> getListMajor() {
		return listMajor;
	}

	public void setListMajor(List<Major> listMajor) {
		this.listMajor = listMajor;
	}

	public List<CourseCategory> getListCourseCate() {
		return listCourseCate;
	}

	public void setListCourseCate(List<CourseCategory> listCourseCate) {
		this.listCourseCate = listCourseCate;
	}

	public List<Course> getListCourse() {
		return listCourse;
	}

	public void setListCourse(List<Course> listCourse) {
		this.listCourse = listCourse;
	}

	public MajorGroup getMajorGroup() {
		return majorGroup;
	}

	public void setMajorGroup(MajorGroup majorGroup) {
		this.majorGroup = majorGroup;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public Major getMajor() {
		return major;
	}

	public void setMajor(Major major) {
		this.major = major;
	}

	public ExcellentCourse getExcellentCourse() {
		return excellentCourse;
	}

	public void setExcellentCourse(ExcellentCourse excellentCourse) {
		this.excellentCourse = excellentCourse;
	}

	public List<MajorFileSrcCate> getListMajorFileSrcCate() {
		return listMajorFileSrcCate;
	}

	public void setListMajorFileSrcCate(
			List<MajorFileSrcCate> listMajorFileSrcCate) {
		this.listMajorFileSrcCate = listMajorFileSrcCate;
	}

	public MajorFileSrcCate getMajorFileSrcCate() {
		return majorFileSrcCate;
	}

	public void setMajorFileSrcCate(MajorFileSrcCate majorFileSrcCate) {
		this.majorFileSrcCate = majorFileSrcCate;
	}

	public Announce getAnnounce() {
		return announce;
	}

	public void setAnnounce(Announce announce) {
		this.announce = announce;
	}

	public List<Announce> getListAnnounce() {
		return listAnnounce;
	}

	public void setListAnnounce(List<Announce> listAnnounce) {
		this.listAnnounce = listAnnounce;
	}

	public List<CourseFileSrcCatePublic> getListCourseFileSrcCatePublic() {
		return listCourseFileSrcCatePublic;
	}

	public void setListCourseFileSrcCatePublic(
			List<CourseFileSrcCatePublic> listCourseFileSrcCatePublic) {
		this.listCourseFileSrcCatePublic = listCourseFileSrcCatePublic;
	}

	public CourseFileSrcCatePublic getCourseFileSrcCatePublic() {
		return courseFileSrcCatePublic;
	}

	public void setCourseFileSrcCatePublic(
			CourseFileSrcCatePublic courseFileSrcCatePublic) {
		this.courseFileSrcCatePublic = courseFileSrcCatePublic;
	}

	public List<CourseTeaClassFK> getListCourseTeaClassFK() {
		return listCourseTeaClassFK;
	}

	public void setListCourseTeaClassFK(
			List<CourseTeaClassFK> listCourseTeaClassFK) {
		this.listCourseTeaClassFK = listCourseTeaClassFK;
	}

	public CourseTeaClassFK getCourseTeaClassFK() {
		return courseTeaFK;
	}

	public void setCourseTeaClassFK(CourseTeaClassFK courseTeaFK) {
		this.courseTeaFK = courseTeaFK;
	}

	public List<Department> getListDepartment() {
		return listDepartment;
	}

	public void setListDepartment(List<Department> listDepartment) {
		this.listDepartment = listDepartment;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

}
